package support.limit.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import support.limit.service.RateLimitAlgorihm;
import support.limit.annotation.RateLimit;
import support.limit.utils.IPUtils;

/**
 * @author:bo
 * @email:
 * @date 2018/12/14 10:52
 * @introduce @RateLimit的切面
 **/
@Slf4j
@Component
@Aspect
public class RateLimitAspect {

    @Autowired
    private RateLimitAlgorihm rateLimitAlgorihm;

    @Pointcut("@annotation(rateLimit)")
    public void annotationPoinCut(RateLimit rateLimit) {
    }

    @Before("annotationPoinCut(rateLimit)")
    public void doBefore(JoinPoint joinPoint, RateLimit rateLimit) {
        String key = IPUtils.getRateKey(joinPoint, rateLimit.rateLimitType());

        // if (StrUtil.isNotBlank(IdentifierThreadLocal.get())) {
        //     entity.setIdentifier(IdentifierThreadLocal.get());
        // }

        // bo TODO 组装参数 ？？
        rateLimitAlgorihm.handler(null);
    }

}
